import ResourceLoader from './ResourceLoader.js';
import Player from '../entities/Player.js';
import Platform from '../entities/Platform.js';
import Carrot from '../entities/Carrot.js';
import Boss from '../entities/Boss.js';
import InputHandler from './InputHandler.js';
import Settings from '../config/Settings.js';
import LevelManager from '../managers/LevelManager.js';
import ScoreManager from '../managers/ScoreManager.js';
import ParticleSystem from '../effects/ParticleSystem.js';
import BackgroundManager from '../managers/BackgroundManager.js';
// 导入weapp-adapter以获取已创建的canvas和system
import { canvas, ctx } from '../libs/weapp-adapter.js';

export default class Game {
  constructor() {
    console.log('Game 构造函数开始执行');
    
    // 获取基本对象
    try {
      // En lugar de crear un nuevo canvas, usamos el existente de weapp-adapter
      this.canvas = canvas;
      this.ctx = ctx;
      
      if (!this.ctx) {
        console.error('Error: No se pudo obtener el contexto de canvas');
        this.ctx = canvas.getContext('2d');
      }
    } catch (error) {
      console.error('创建画布失败:', error);
    }
    
    // 获取系统信息
    try {
      this.systemInfo = wx.getSystemInfoSync();
      console.log('系统信息:', this.systemInfo);
    } catch (error) {
      console.error('获取系统信息失败:', error);
      // 设置默认值
      this.systemInfo = {
        windowWidth: 932,
        windowHeight: 430,
        pixelRatio: 3
      };
    }
    
    // 设计尺寸
    this.designWidth = 932;
    this.designHeight = 430;
    
    // 实际屏幕尺寸
    this.gameWidth = this.systemInfo.windowWidth;
    this.gameHeight = this.systemInfo.windowHeight;
    console.log('屏幕尺寸:', {
      width: this.gameWidth, 
      height: this.gameHeight,
      ratio: this.systemInfo.pixelRatio
    });
    
    // 添加调试模式标志 - 设置为true启用全部视觉效果和调试功能
    this.debugMode = true;
    
    // 游戏状态
    this.logRenderInfoTimer = 0;
    this.state = 'loading'; // loading, running, paused, gameOver, victory
    this.gameTime = 0;
    
    // 背景和地面颜色
    this.backgroundColor = '#87CEEB'; // 默认天蓝色背景
    this.groundColor = '#8B4513';     // 默认棕色地面
    
    // 初始化资源加载器
    this.resourceLoader = new ResourceLoader();
    
    // 初始化管理器
    this.levelManager = new LevelManager(this);
    this.scoreManager = new ScoreManager();
    this.backgroundManager = new BackgroundManager(this);
    
    // 初始化输入处理
    this.inputHandler = new InputHandler(this);
    
    // 游戏实体
    this.player = null;
    
    // 初始化粒子系统
    this.particleSystem = new ParticleSystem(this);
    
    // 初始化浮动文本数组
    this.floatingTexts = [];
    
    console.log('Game 构造函数执行完成');
  }
  
  // 游戏初始化
  async init() {
    try {
      // 加载资源
      const resourcesLoaded = await this.resourceLoader.loadAll();
      
      if (!resourcesLoaded) {
        console.error('资源加载失败');
        return false;
      }
      
      // 创建玩家
      this.player = new Player(this, {
        x: this.designWidth / 3,
        y: this.designHeight - 150,  // 将玩家位置提高，距离底部150像素
        width: 80,
        height: 80
      });
      
      // 初始化关卡
      this.levelManager.init();
      
      // 直接设置为运行状态
      this.state = 'running';
      
      return true;
    } catch (error) {
      console.error('游戏初始化错误:', error);
      return false;
    }
  }
  
  // 启动游戏
  async start() {
    const initialized = await this.init();
    
    if (!initialized) {
      console.error('游戏初始化失败，无法启动');
      return;
    }
    
    console.log('游戏初始化完成');
    this.gameLoop();
  }
  
  // 游戏循环
  gameLoop() {
    try {
      // 使用requestAnimationFrame保持循环
      wx.requestAnimationFrame(() => this.gameLoop());
      
      // 更新帧率计数
      const now = Date.now();
      this.frameCount++;
      
      if (now - this.lastFpsUpdateTime >= 1000) {
        this.fps = this.frameCount;
        this.frameCount = 0;
        this.lastFpsUpdateTime = now;
        
        // 每秒输出FPS
        console.log(`FPS: ${this.fps}`);
      }
      
      // 只有在游戏运行时更新游戏逻辑
      if (this.state === 'running') {
        this.update();
        this.gameTime++;
        
        // 更新日志计时器
        this.logRenderInfoTimer = (this.logRenderInfoTimer + 1) % 300;
      }
      
      // 总是渲染画面，即使在暂停或其他状态
      this.render();
    } catch (error) {
      console.error('Game loop error:', error);
      
      // 游戏循环发生严重错误时显示错误恢复UI
      this.showErrorRecoveryUI('游戏运行出错，点击继续', () => {
        // 重置一些状态，尝试恢复
        this.gameTime = Math.max(0, this.gameTime - 60); // 回退1秒
        this.state = 'running';
      });
    }
  }
  
  // 更新游戏状态
  update() {
    try {
      // 增加游戏时间计数
      this.gameTime++;
      
      // 如果游戏没有在运行状态，不执行以下更新
      if (this.state !== 'running') return;
      
      // 更新玩家状态
      if (this.player) {
        this.player.update();
      }
      
      // 更新关卡
      if (this.levelManager) {
        this.levelManager.update();
      }
      
      // 更新背景
      if (this.backgroundManager) {
        this.backgroundManager.update();
      }
      
      // 更新浮动文本
      if (this.floatingTexts && this.floatingTexts.length > 0) {
        this.updateFloatingTexts();
      }
      
      // 更新粒子系统
      if (this.particleSystem) {
        this.particleSystem.update();
      }
      
      // 检查游戏状态
      this.checkGameState();
      
      // 每60帧输出一次调试信息
      if (this.gameTime % 60 === 0) {
        console.log('Game Loop:', {
          state: this.state,
          time: this.gameTime,
          playerPos: this.player ? { x: this.player.x, y: this.player.y } : null
        });
      }
    } catch (error) {
      console.error('Game update error:', error);
      
      // 尝试恢复游戏状态
      if (this.state === 'running') {
        // 暂停游戏而不是立即结束，给玩家一个恢复的机会
        this.state = 'paused';
        // 创建一个恢复按钮，允许玩家继续游戏
        this.showErrorRecoveryUI('游戏出现问题，点击继续尝试恢复', () => {
          this.state = 'running';
        });
      }
    }
  }
  
  // 渲染游戏
  render() {
    try {
      // 如果上下文不存在，跳过渲染
      if (!this.ctx) {
        console.error('渲染失败: 上下文不存在');
        return;
      }
      
      // 清空画布
      this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
      
      // 绘制背景色
      this.ctx.fillStyle = this.backgroundColor || '#87CEEB';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      // 渲染流程根据游戏状态
      if (this.state === 'loading') {
        this.renderLoadingScreen();
        return;
      }
      
      // 绘制背景元素
      if (this.backgroundManager) {
        this.backgroundManager.render(this.ctx);
      }
      
      // 渲染关卡
      if (this.levelManager) {
        this.levelManager.render(this.ctx);
      }
      
      // 渲染玩家
      if (this.player) {
        this.player.render(this.ctx);
      }
      
      // 渲染粒子系统
      if (this.particleSystem) {
        this.particleSystem.render(this.ctx);
      }
      
      // 渲染浮动文本
      this.renderFloatingTexts();
      
      // 渲染UI (包含暂停、游戏结束等画面)
      this.renderUI();
      
      // 渲染错误UI（如果有）
      if (this.hasError && this.errorMessage) {
        this.renderErrorUI();
      }
      
      // 每180帧输出一次渲染信息
      if (this.gameTime % 180 === 0) {
        console.log('渲染帧:', {
          time: this.gameTime,
          playerPos: this.player ? { x: this.player.x, y: this.player.y } : null,
          state: this.state
        });
      }
    } catch (error) {
      console.error('Game render error:', error);
      
      // 尝试最基本的恢复渲染
      try {
        if (this.ctx) {
          // 绘制一个简单的背景和错误消息，避免黑屏
          this.ctx.fillStyle = '#333333';
          this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
          
          this.ctx.fillStyle = '#FF0000';
          this.ctx.font = '20px Arial';
          this.ctx.textAlign = 'center';
          this.ctx.fillText('渲染错误，请重启游戏', this.canvas.width / 2, this.canvas.height / 2);
          this.ctx.textAlign = 'left';
        }
      } catch (e) {
        // 如果连基本恢复都失败，只能记录错误
        console.error('无法恢复渲染:', e);
      }
    }
  }
  
  // 渲染UI
  renderUI() {
    // 分数和胡萝卜显示已移除（根据用户要求）
    // 原先显示在左上角的UI元素已被删除
    
    // 显示生命数（用小兔子图标）
    const lives = this.player.lives || 3; // 默认3条命
    const rabbitImage = this.getImage('rabbit');
    
    if (rabbitImage && !rabbitImage.isPlaceholder) {
      try {
        for (let i = 0; i < lives; i++) {
          this.ctx.drawImage(
            rabbitImage,
            0, 0, // 使用第一帧
            Settings.spriteConfig.frameWidth,
            Settings.spriteConfig.frameHeight,
            this.canvas.width - 40 - (i * 35), 20, 
            30, 30
          );
        }
      } catch (e) {
        // 如果图片绘制失败，使用文字
        this.ctx.fillText(`生命: ${lives}`, this.canvas.width - 100, 40);
      }
    } else {
      this.ctx.fillText(`生命: ${lives}`, this.canvas.width - 100, 40);
    }
    
    // 游戏状态提示
    if (this.state === 'gameOver') {
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.font = '36px Arial';
      this.ctx.textAlign = 'center';
      this.ctx.fillText('游戏结束', this.canvas.width / 2, this.canvas.height / 2 - 40);
      this.ctx.fillText(`最终分数: ${this.scoreManager.getScore()}`, this.canvas.width / 2, this.canvas.height / 2);
      this.ctx.font = '24px Arial';
      this.ctx.fillText('点击屏幕重新开始', this.canvas.width / 2, this.canvas.height / 2 + 40);
      this.ctx.textAlign = 'left';
    }
    
    // 游戏胜利提示
    if (this.state === 'victory') {
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      this.ctx.fillStyle = '#FFEB3B';
      this.ctx.font = '48px Arial';
      this.ctx.textAlign = 'center';
      this.ctx.fillText('恭喜通关!', this.canvas.width / 2, this.canvas.height / 2 - 60);
      
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.font = '36px Arial';
      this.ctx.fillText(`最终分数: ${this.scoreManager.getScore()}`, this.canvas.width / 2, this.canvas.height / 2);
      this.ctx.fillText(`收集胡萝卜: ${this.scoreManager.getCarrots()}`, this.canvas.width / 2, this.canvas.height / 2 + 50);
      
      this.ctx.font = '28px Arial';
      this.ctx.fillText('点击屏幕重新开始', this.canvas.width / 2, this.canvas.height / 2 + 120);
      this.ctx.textAlign = 'left';
    }
    
    // 绘制暂停按钮
    if (this.state === 'running') {
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.5)';
      this.ctx.fillRect(this.canvas.width - 50, 10, 40, 40);
      
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.fillRect(this.canvas.width - 40, 15, 7, 30);
      this.ctx.fillRect(this.canvas.width - 27, 15, 7, 30);
    }
    
    // 暂停菜单
    if (this.state === 'paused') {
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.font = '36px Arial';
      this.ctx.textAlign = 'center';
      this.ctx.fillText('游戏暂停', this.canvas.width / 2, this.canvas.height / 3);
      
      // 继续按钮
      this.drawButton('继续游戏', this.canvas.width / 2, this.canvas.height / 2, 200, 50);
      
      // 返回主菜单
      this.drawButton('返回主菜单', this.canvas.width / 2, this.canvas.height / 2 + 70, 200, 50);
      
      this.ctx.textAlign = 'left';
    }
  }
  
  // 绘制按钮
  drawButton(text, x, y, width, height) {
    this.ctx.fillStyle = '#4CAF50';
    this.ctx.fillRect(x - width/2, y - height/2, width, height);
    
    this.ctx.fillStyle = '#FFFFFF';
    this.ctx.font = '24px Arial';
    this.ctx.textAlign = 'center';
    this.ctx.textBaseline = 'middle';
    this.ctx.fillText(text, x, y);
    this.ctx.textBaseline = 'alphabetic';
  }
  
  // 检查游戏状态
  checkGameState() {
    // 检查玩家是否掉出屏幕
    if (this.player.y > this.gameHeight) {
      this.gameOver();
    }
  }
  
  // 游戏结束
  gameOver() {
    this.state = 'gameOver';
  }
  
  // 重置游戏
  reset() {
    this.gameTime = 0;
    this.state = 'running';
    
    // 重置分数
    this.scoreManager.reset();
    
    // 重置玩家
    this.player.reset();
    
    // 重置关卡
    this.levelManager.reset();
  }
  
  // 获取资源
  getImage(name) {
    return this.resourceLoader.getImage(name);
  }
  
  // 处理屏幕旋转
  handleOrientationChange(orientation) {
    let windowInfo;
    try {
      windowInfo = wx.getWindowInfo();
    } catch (e) {
      windowInfo = wx.getSystemInfoSync();
    }
    
    let screenWidth = windowInfo.windowWidth;
    let screenHeight = windowInfo.windowHeight;
    
    if (orientation !== 'landscape') {
      [screenWidth, screenHeight] = [screenHeight, screenWidth];
    }
    
    // 重新计算缩放和偏移（考虑像素比）
    const scaleX = (screenWidth * this.systemInfo.pixelRatio) / this.designWidth;
    const scaleY = (screenHeight * this.systemInfo.pixelRatio) / this.designHeight;
    this.scale = scaleX;  // 在横屏模式下，使用宽度缩放
    
    // 更新画布尺寸
    this.canvas.width = screenWidth * this.systemInfo.pixelRatio;
    this.canvas.height = screenHeight * this.systemInfo.pixelRatio;
    
    // 更新偏移量
    this.offsetX = 0;  // 水平不需要偏移
    this.offsetY = ((screenHeight * this.systemInfo.pixelRatio) - (this.designHeight * this.scale)) / 2;
    
    // 重置画布缩放
    this.ctx.scale(this.systemInfo.pixelRatio, this.systemInfo.pixelRatio);
  }
  
  // 在Game.js中添加胜利方法
  victory() {
    this.state = 'victory';
  }
  
  // 添加坐标转换方法
  getActualX(x) {
    return (x * this.systemInfo.pixelRatio - this.offsetX) / this.scale;
  }
  
  getActualY(y) {
    return (y * this.systemInfo.pixelRatio - this.offsetY) / this.scale;
  }
  
  // 获取游戏边界
  getGameBounds() {
    return {
      left: 0,
      right: this.gameWidth,
      top: 0,
      bottom: this.gameHeight
    };
  }
  
  updateFloatingTexts() {
    for (let i = this.floatingTexts.length - 1; i >= 0; i--) {
      const text = this.floatingTexts[i];
      text.y += text.velocity;
      text.alpha -= 0.02;
      
      if (text.alpha <= 0) {
        this.floatingTexts.splice(i, 1);
      }
    }
  }
  
  renderFloatingTexts() {
    for (const text of this.floatingTexts) {
      this.ctx.save();
      this.ctx.globalAlpha = text.alpha;
      this.ctx.fillStyle = text.color;
      this.ctx.font = '24px Arial';
      this.ctx.textAlign = 'center';
      this.ctx.fillText(text.content, text.x, text.y);
      this.ctx.restore();
    }
  }
  
  // 渲染错误UI
  renderErrorUI() {
    try {
      // 半透明背景
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      // 错误消息
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.font = '24px Arial';
      this.ctx.textAlign = 'center';
      this.ctx.fillText(this.errorMessage, this.canvas.width / 2, this.canvas.height / 2 - 40);
      
      // 重试按钮
      this.drawButton('继续游戏', this.canvas.width / 2, this.canvas.height / 2 + 20, 200, 50);
      
      // 检查错误UI回调是否已经设置
      if (this.inputHandler && !this.errorCallbackSet) {
        this.inputHandler.setClickCallback((x, y) => {
          const buttonX = this.canvas.width / 2;
          const buttonY = this.canvas.height / 2 + 20;
          const buttonWidth = 200;
          const buttonHeight = 50;
          
          if (x >= buttonX - buttonWidth/2 && x <= buttonX + buttonWidth/2 &&
              y >= buttonY - buttonHeight/2 && y <= buttonY + buttonHeight/2) {
            // 清除错误状态
            this.hasError = false;
            this.errorMessage = null;
            this.errorCallbackSet = false;
            
            // 执行恢复回调
            if (typeof this.errorRetryCallback === 'function') {
              this.errorRetryCallback();
            }
            
            // 重置点击回调
            this.inputHandler.clearClickCallback();
          }
        });
        
        // 标记回调已设置
        this.errorCallbackSet = true;
      }
    } catch (error) {
      console.error('渲染错误UI失败:', error);
    }
  }
  
  // 显示错误恢复UI
  showErrorRecoveryUI(message, retryCallback) {
    this.hasError = true;
    this.errorMessage = message;
    this.errorRetryCallback = retryCallback;
    this.errorCallbackSet = false; // 重置回调设置标志
    
    // 立即渲染错误UI
    if (this.ctx) {
      this.renderErrorUI();
    }
  }
  
  // 渲染加载屏幕
  renderLoadingScreen() {
    try {
      // 绘制背景色
      this.ctx.fillStyle = '#000000';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      // 绘制加载进度条
      const progressWidth = 400;
      const progressHeight = 20;
      const progressX = (this.canvas.width - progressWidth) / 2;
      const progressY = this.canvas.height / 2;
      
      // 进度条容器
      this.ctx.strokeStyle = '#FFFFFF';
      this.ctx.lineWidth = 2;
      this.ctx.strokeRect(progressX, progressY, progressWidth, progressHeight);
      
      // 进度条填充
      const progress = this.resourceLoader ? this.resourceLoader.getProgress() : 0.5;
      this.ctx.fillStyle = '#00FF00';
      this.ctx.fillRect(progressX + 2, progressY + 2, (progressWidth - 4) * progress, progressHeight - 4);
      
      // 加载文本
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.font = '24px Arial';
      this.ctx.textAlign = 'center';
      this.ctx.fillText('加载中...', this.canvas.width / 2, progressY - 20);
      this.ctx.fillText(`${Math.floor(progress * 100)}%`, this.canvas.width / 2, progressY + progressHeight + 30);
      this.ctx.textAlign = 'left';
    } catch (error) {
      console.error('渲染加载屏幕错误:', error);
    }
  }
  
  // 渲染暂停屏幕
  renderPauseScreen() {
    try {
      // 半透明背景
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      // 暂停文本
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.font = '48px Arial';
      this.ctx.textAlign = 'center';
      this.ctx.fillText('游戏暂停', this.canvas.width / 2, this.canvas.height / 3);
      
      // 继续游戏按钮
      this.drawButton('继续游戏', this.canvas.width / 2, this.canvas.height / 2, 200, 50);
      
      // 返回主菜单按钮
      this.drawButton('返回主菜单', this.canvas.width / 2, this.canvas.height / 2 + 70, 200, 50);
      
      this.ctx.textAlign = 'left';
    } catch (error) {
      console.error('渲染暂停屏幕错误:', error);
    }
  }
  
  // 渲染游戏结束屏幕
  renderGameOverScreen() {
    try {
      // 半透明背景
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      // 游戏结束文本
      this.ctx.fillStyle = '#FF0000';
      this.ctx.font = '48px Arial';
      this.ctx.textAlign = 'center';
      this.ctx.fillText('游戏结束', this.canvas.width / 2, this.canvas.height / 3);
      
      // 分数
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.font = '36px Arial';
      const score = this.scoreManager ? this.scoreManager.getScore() : 0;
      this.ctx.fillText(`最终得分: ${score}`, this.canvas.width / 2, this.canvas.height / 2);
      
      // 重新开始按钮
      this.drawButton('重新开始', this.canvas.width / 2, this.canvas.height / 2 + 100, 200, 50);
      
      this.ctx.textAlign = 'left';
    } catch (error) {
      console.error('渲染游戏结束屏幕错误:', error);
    }
  }
  
  // 渲染胜利屏幕
  renderVictoryScreen() {
    try {
      // 半透明背景
      this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)';
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
      
      // 胜利文本
      this.ctx.fillStyle = '#FFD700'; // 金色
      this.ctx.font = '48px Arial';
      this.ctx.textAlign = 'center';
      this.ctx.fillText('恭喜通关!', this.canvas.width / 2, this.canvas.height / 3);
      
      // 分数
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.font = '36px Arial';
      const score = this.scoreManager ? this.scoreManager.getScore() : 0;
      this.ctx.fillText(`最终得分: ${score}`, this.canvas.width / 2, this.canvas.height / 2);
      
      // 重新开始按钮
      this.drawButton('再玩一次', this.canvas.width / 2, this.canvas.height / 2 + 100, 200, 50);
      
      this.ctx.textAlign = 'left';
    } catch (error) {
      console.error('渲染胜利屏幕错误:', error);
    }
  }
  
  // 渲染分数
  renderScore() {
    try {
      if (!this.scoreManager) return;
      
      this.ctx.fillStyle = '#FFFFFF';
      this.ctx.font = '24px Arial';
      this.ctx.textAlign = 'left';
      this.ctx.fillText(`分数: ${this.scoreManager.getScore()}`, 20, 30);
      
      // 渲染胡萝卜数量
      this.ctx.fillText(`胡萝卜: ${this.scoreManager.getCarrots()}`, 20, 60);
      
      // 渲染连击数
      if (this.levelManager && this.levelManager.currentCombo > 1) {
        this.ctx.fillStyle = '#FFD700'; // 金色
        this.ctx.fillText(`连击: ${this.levelManager.currentCombo}x`, 20, 90);
      }
      
      // 渲染暂停按钮
      this.ctx.fillStyle = '#CCCCCC';
      this.ctx.fillRect(this.canvas.width - 50, 10, 40, 40);
      this.ctx.fillStyle = '#000000';
      this.ctx.fillRect(this.canvas.width - 40, 15, 7, 30);
      this.ctx.fillRect(this.canvas.width - 27, 15, 7, 30);
    } catch (error) {
      console.error('渲染分数错误:', error);
    }
  }
  
  // 添加状态切换方法
  setState(newState) {
    console.log(`Cambiando estado del juego: ${this.state} -> ${newState}`);
    
    // 特殊状态转换逻辑
    if (this.state === 'paused' && newState === 'running') {
      // 从暂停恢复到运行时，确保清除任何暂停相关的回调
      if (this.inputHandler) {
        this.inputHandler.clearClickCallback();
      }
    }
    
    // 更新状态
    this.state = newState;
    
    // 根据新状态执行特定操作
    if (newState === 'paused') {
      console.log('Juego pausado');
    } else if (newState === 'running') {
      console.log('Juego en ejecución');
    }
  }
} 